﻿using System.IO;
using System.Windows;
using GalaSoft.MvvmLight.Messaging;
using ProjectWizardManagementv1.ViewModels.Concrete;

namespace ProjectWizardManagementv1.Views
{
    /// <summary>
    /// Interaction logic for RenameFileWindow.xaml
    /// </summary>
    public partial class RenameFileWindow : Window
    {
        private static readonly char[] FORBIDDEN_CHARACTERS = new []{'\\', '|', '/', ':', '*', '?', '"', '<', '>'};
        private ProjectFileViewModel _file;

        public string Filename 
        { get { return _file.Name; }}

        public ProjectFileViewModel File { get; set; }

        public RenameFileWindow()
        {
            InitializeComponent();
            File = (ProjectFileViewModel) DataContext;
        }

        public RenameFileWindow(ProjectFileViewModel file)
        {
            InitializeComponent();
            _file = file;
            File = file;
        }

        private void SendRenamedFile(object sender, RoutedEventArgs e)
        {
            var newName = this.RenameTextBox.Text;

            if(newName.IndexOfAny(FORBIDDEN_CHARACTERS) == -1)
            {
                string newPath = Directory.GetParent(File.Path).FullName + "\\" + newName + File.Extension;
                Messenger.Default.Send(new RenameFileMessage(File.Path, newPath, File)); 
                this.Close();
            }
            else
            {
                MessageBox.Show("Please avoid the following characters: \\ | / : * ? \" < > ", "Forbidden Characters",
                                MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        private void CloseWindow(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
